<template>
	<view style="padding-top: 20rpx;">
		<view class="audio-box">
			<view class="info-box">
				<view class="head-box" @click="playFun">
					<image v-if="pubData.image" :src="pubData.image" class="img" :class="playShowStatus==2?'xuanz':''"></image>
					<image v-else :src="localIconSrc('17.png')" class="img" :class="playShowStatus==2?'xuanz':''"></image>
					<view class="play-box" v-if="playShowStatus !=1">
						<view class="ico"><u-icon name="play-circle" color="#ffffff" size="28"
								v-if="playShowStatus==3"></u-icon></view>
						<view class="ico"><u-icon name="pause-circle" color="#ffffff" size="28"
								v-if="playShowStatus==2"></u-icon></view>
					</view>
				</view>
				<view class="content">
					<view class="t1">{{pubData.inputVal}}</view>
					<view>mp3</view>
				</view>
			</view>
			<view class="loading-box" v-if="playShowStatus==1"></view>
			<view class="button" v-if="audioUrl">
				<view class="btn" @click="downloadAudio">下载音频</view>
				<!-- <view class="btn" @click="downloadText">下载字幕</view> -->
			</view>
		</view>
		<view class="input-content">{{pubData.inputVal}}</view>
	</view>
</template>

<script>
	import {
		fileLinkUrl,
		audioData
	} from "@/api/index.js"
	export default {
		data() {
			return {
				playShowStatus: 1,
				pubData: {
					inputVal:''
				},
				audioUrl: '',
				audioUrls: '',
				playAudio: null,
				flag: false,
				requestFlag: null
			};
		},
		onLoad(option) {
			this.playAudio = uni.createInnerAudioContext()
			if (option && option.item) {
				let str = JSON.parse(option.item)
				this.pubData.inputVal = str.text
				this.audioUrl=`https:${str.audio_url}`
				console.log('经济拉萨的开发',this.pubData)
				this.playAudio.src = `https:${str.audio_url}`;
				this.playAudio.play(); // 开始播放音频
				this.playAudio.onCanplay(() => {
					console.log('可以播放')
					this.playAudio.play()
					this.playShowStatus = 2
				})
				this.playAudio.onEnded(() => {
					this.playShowStatus = 3
					console.log('放完了2')
				})
			} else {
				const pub = getApp().globalData.pubData
				this.pubData = pub
				this.getAlyUrl(pub.taskid)
				// this.getAudio(pub.taskid)
				return
				// this.audioUrl = fileLinkUrl + '?taskid=' + getApp().globalData.pubData.taskid
				console.log(this.audioUrl)
				// #ifdef H5
				this.playAudio.src = this.audioUrl;
				this.playAudio.play(); // 开始播放音频
				this.playAudio.onCanplay(() => {
					console.log('可以播放')
					this.playAudio.play()
					this.playShowStatus = 2
				})
				this.playAudio.onEnded(() => {
					this.playShowStatus = 3
					console.log('放完了2')
				})
				// #endif
				// #ifdef MP-WEIXIN

				this.dataInf()
				// #endif
			}


		},
		onHide() {
			this.flag = false
			if (this.playAudio) {
				this.playAudio.stop()
			}
		},
		onUnload() {
			if (this.requestFlag) {
				this.requestFlag.abort()
			}
			this.flag = false
			if (this.playAudio) {
				this.playAudio.stop()
				this.playAudio.destroy()
			}
		},
		methods: {
			getAlyUrl(id) {
				this.requestFlag = uni.request({
					url: fileLinkUrl,
					method: "GET",
					data: {
						taskid: id
					},
					success: (res) => {
						if (res.data.domain) {
							this.audioUrl = `https://${res.data.domain}/${res.data.audio_path}`;
							this.playAudio.src = `https://${res.data.domain}/${res.data.audio_path}`;
							this.playAudio.play(); // 开始播放音频
							this.playAudio.onCanplay(() => {
								console.log('可以播放')
								this.playAudio.play()
								this.playShowStatus = 2
							})
							this.playAudio.onEnded(() => {
								this.playShowStatus = 3
								console.log('放完了2')
							})
						} else {
							uni.showToast({
								title: res.data.errmsg,
								icon: 'none'
							})
						}


					},
					fail() {
						console.log('失败')
					}
				})
			},
			getAudio(id) {
				this.requestFlag = uni.request({
					url: fileLinkUrl,
					method: "GET",
					data: {
						taskid: id
					},
					success: (res) => {
						console.log(res)


					},
					fail() {
						console.log('失败')
					}
				})
				this.flag = true
				this.getAudios(id)

			},
			getAudios(id) {
				let that = this
				audioData({
					taskid: id
				}).then(res => {
					if (res.errcode == 200) {
						that.playAudio.src = `https://${res.data.domian}/${res.data.audio_path}`;
						that.audioUrl = `https://${res.data.domian}/${res.data.audio_path}`
						that.playAudio.play(); // 开始播放音频

						that.playAudio.onCanplay(() => {
							console.log('可以播放')
							that.playAudio.play()
							that.playShowStatus = 2
						})

						that.playAudio.onPlay(() => {
							console.log('出发了')
							that.playShowStatus = 2
						})
						that.playAudio.onCanplay(() => {
							console.log('播放了')
							that.playShowStatus = 2
						})
						that.playAudio.onPause(() => {
							that.playShowStatus = 3
							console.log('放完了')

						})
						that.playAudio.onEnded(() => {
							that.playShowStatus = 3
							console.log('放完了2')
						})
					} else {
						if (that.flag) {
							setTimeout(() => {
								that.getAudios(that.pubData.taskid)
							}, 2000)
						}

					}
				})
			},
			downloadAudio() {
				uni.showLoading({
					title: '请稍后...'
				})
				// 预览和下载
				let that = this
				uni.downloadFile({
					url: that.audioUrl,
					timeout: 100000,
					success: (res) => {
						uni.hideLoading()
						uni.shareFileMessage({
							filePath: res.tempFilePath,
							fileName: 'mp3',
							success: () => {

							}
						})
					}
				})

			},
			downloadText() {
				uni.showToast({
					title: '请稍后再试',
					icon: 'none'
				})
			},
			playFun() {
				if (this.playAudio) {
					if (this.playShowStatus == 3) {
						this.playShowStatus = 2
						this.playAudio.play()
					} else if (this.playShowStatus == 2) {
						this.playShowStatus = 3
						this.playAudio.stop()
					} else {

					}
				}
			},
			dataInf() {
				const that = this
				uni.request({
					url: that.audioUrl, //调用后端接口
					method: "GET",
					//获取文件流
					responseType: 'arraybuffer',
					success(res) {
						// const path = `${wx.env.USER_DATA_PATH}/12145.mp3`;
						const path = `${wx.env.USER_DATA_PATH}/${getApp().globalData.pubData.taskid}.mp3`;
						console.log(path)
						const fsm = wx.getFileSystemManager();
						fsm.writeFile({
							filePath: path,
							data: res.data,
							encoding: 'base64',
							success: (res) => {
								that.playAudio.src = path;
								that.audioUrls = path
								that.playAudio.play(); // 开始播放音频

								that.playAudio.onCanplay(() => {
									console.log('可以播放')
									that.playAudio.play()
									that.playShowStatus = 2
								})

								that.playAudio.onPlay(() => {
									console.log('出发了')
									that.playShowStatus = 2
								})
								that.playAudio.onCanplay(() => {
									console.log('播放了')
									that.playShowStatus = 2
								})
								that.playAudio.onPause(() => {
									that.playShowStatus = 3
									console.log('放完了')

								})
								that.playAudio.onEnded(() => {
									that.playShowStatus = 3
									console.log('放完了2')
								})

							},
						});
					},
					fail(res) {
						console.log(res);
					}
				})

			}
		}
	}
</script>

<style lang="scss" scoped>
	@keyframes loading {
		0% {
			width: 0;
		}

		100% {
			width: 100%;
		}
	}
	@keyframes xxx {
		0%{
			transform: rotate(0deg);
		}
		100%{
			transform: rotate(360deg);
		}
	}
	.xuanz{
		animation: xxx 3s infinite linear;
	}

	.audio-box {
		// height: 140rpx;
		width: 92%;
		position: relative;
		margin: auto;
		background-color: #fff;
		padding: 20rpx 20rpx;
		box-sizing: border-box;
		border-radius: 10rpx;

		.info-box {
			display: flex;

			.content {
				margin-left: 20rpx;
				font-size: 26rpx;

				.t1 {
					width: 460rpx;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}
			}

			.head-box {
				height: 100rpx;
				width: 100rpx;
				border-radius: 50%;
				position: relative;

				.play-box {
					height: 100%;
					width: 100%;
					position: absolute;
					left: 0;
					top: 0;
					border-radius: 50%;
					background-color: rgba(0, 0, 0, 0.4);

					.ico {
						position: absolute;
						top: 50%;
						left: 50%;
						transform: translate(-50%, -50%);
					}
				}

				.img {
					height: 100%;
					width: 100%;
					border-radius: 50%;
				}
			}

		}

		.loading-box {
			height: 1px;
			// width: 100%;
			background-color: #cacaca;
			animation: loading 1s infinite;
			position: absolute;
			bottom: 0;
			left: 50%;
			transform: translateX(-50%);
		}

		.button {
			display: flex;
			flex-direction: row-reverse;
			height: 80rpx;
			border-top: 1px solid #f4f4f4;
			margin-top: 20rpx;

			.btn {
				height: 60rpx;
				width: 160rpx;
				line-height: 60rpx;
				text-align: center;
				border-radius: 30rpx;
				color: #{$main-color};
				border: 1px solid #{$main-color};
				margin-top: 20rpx;
				margin-left: 20rpx;
				font-size: 24rpx;
			}

		}
	}





	.input-content {
		background-color: #fff;
		border-radius: 10rpx;
		width: 92%;
		margin: auto;
		margin-top: 20rpx;
		padding: 20rpx 20rpx;
		box-sizing: border-box;
		height: calc(100vh - 350rpx);
		// overflow-y: scroll;
		color: #484848;
		font-size: 26rpx;
	}
</style>